from sanic.request import File
import os
from app.utils.filesystem.StorageDriver import StorageDriver


class LocalFile(StorageDriver):
    def download():
        pass
    def upload(self, file:File, filename=None):
        if filename==None:
            filename=file.name
        if filename[0:1]!='/':
            filename="/"+filename
        root="/public/storage"+self.config['root']+filename
        rootArr=root.split("/")
        filedir=os.path.abspath(".")+os.sep+os.path.join(*rootArr[:-1])
        if not os.path.exists(filedir):
            os.makedirs(filedir)
        savePath=os.path.abspath(".")+os.sep+os.path.join(*rootArr)
        with open(savePath, 'wb') as f:
            f.write(file.body)
        return root
